package com.ly.demo.taach.jdbc.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTable;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.stream.Collectors;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;

import com.ly.demo.taach.jdbc.dao.IDeptDao;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DeptWin {

	private JFrame frame;
	private JTextField tf_dname;
	private JTextField tf_loc;
	private JTable table;

	private Vector<Vector<Object>> data = new Vector<>();
	private Vector<Object> head = new Vector<>();

	/**
	 * 	部门数据库操作对象
	 */
	private IDeptDao dao;

	/**
	 * 	主方法
	 * @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public static void main(String[] args) throws Exception {
		String IDeptDao实现类路径 = "com.ly.demo.taach.jdbc.impl.DeptDao";
		// 动态创建IDeptDao实现类
		IDeptDao dao = (IDeptDao) Class.forName(IDeptDao实现类路径).newInstance();
		DeptWin win = new DeptWin(dao);
		win.open();
	}

	public void open() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public DeptWin(IDeptDao dao) {
		this.dao = dao;
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("部门管理");
		frame.setBounds(100, 100, 694, 464);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		frame.getContentPane().add(panel, BorderLayout.NORTH);

		JLabel lblNewLabel = new JLabel("部门名称：");
		panel.add(lblNewLabel);

		tf_dname = new JTextField();
		panel.add(tf_dname);
		tf_dname.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("部门地址：");
		panel.add(lblNewLabel_1);

		tf_loc = new JTextField();
		panel.add(tf_loc);
		tf_loc.setColumns(10);

		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				query();
			}
		});
		panel.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("新增");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new DeptEdit();
			}
		});
		panel.add(btnNewButton_1);

		JButton btnNewButton_2 = new JButton("修改");
		panel.add(btnNewButton_2);

		JButton btnNewButton_3 = new JButton("删除");
		panel.add(btnNewButton_3);

		head.add("部门编号");
		head.add("部门名称");
		head.add("部门地址");
		table = new JTable(data, head);
		table.getColumnModel().getColumn(0).setPreferredWidth(163);
		table.getColumnModel().getColumn(1).setPreferredWidth(170);
		table.getColumnModel().getColumn(2).setPreferredWidth(167);
		frame.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
		frame.pack();
		frame.setLocationRelativeTo(null);
		query();
	}

	private void query() {
		try {
			List<Map<String, Object>> list = dao.select(tf_dname.getText(), tf_loc.getText());
			data.clear();
			list.forEach(d -> {
				data.add(new Vector<>(d.values()));
			});
			table.updateUI();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
